package com.main.login.interfaces.facade;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import com.jcraft.jsch.*;
import com.main.common.base.resp.RespBody;
import com.main.common.interfaces.SessionLocale;
import com.main.common.utils.SaTokenUtil;
import com.main.common.utils.exceptions.StackTraceLog;
import com.main.login.application.service.AccountService;
import com.main.login.interfaces.dto.AddAccountDTO;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@RestController
@SessionLocale
public class LoginFacade {

    @Resource
    private AccountService accountService;

    private final SaTokenUtil saTokenUtil = new SaTokenUtil();

    @PostMapping("/doLogin")
    public RespBody<String> doLogin(@Validated @RequestBody AddAccountDTO addAccountDTO) {
        if("zhang1".equals(addAccountDTO.getNumber()) && "123456".equals(addAccountDTO.getPassword())) {
            saTokenUtil.Login("10001","pc");
            return RespBody.ok("登录成功");
        }
        if("zhang0".equals(addAccountDTO.getNumber()) && "123456".equals(addAccountDTO.getPassword())) {
            saTokenUtil.Login("10000","android");
            return RespBody.ok("登录成功");
        }
        if("zhang".equals(addAccountDTO.getNumber()) && "123456".equals(addAccountDTO.getPassword())) {
            saTokenUtil.Login("10000","pc");
            return RespBody.ok("登录成功");
        }
        return RespBody.fail("登录失败");
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @GetMapping("/isLogin")
    public RespBody<String> isLogin() {
        // 获取当前会话的token
        String tokenValue = StpUtil.getTokenValue();
        System.out.println(tokenValue);
        // 查询会话信息
        SaSession session = StpUtil.getSession();
        System.out.println("sessionId");
        System.out.println(session.getId());
//        try {
//            String[] command = {"cmd","/c","start","mesrobot.bat"};
//            Runtime.getRuntime().exec(command,null,new File("C:\\Users\\29126\\Desktop\\mesrobot-jar\\"));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        return RespBody.ok("当前会话是否登录：" + StpUtil.isLogin());
    }

    @Autowired
    private SftpUploadService sftpUploadService;
    @GetMapping("/upload")
    public RespBody<String> upload() throws JSchException, SftpException {
        String filePath = "C:\\local\\file.txt";
            sftpUploadService.uploadFile(filePath);
            return RespBody.ok("成功");
    }


}
